package com.online.messagebatchmanageserver.controller;

import com.online.messagebatchmanageserver.entity.Message;
import com.online.messagebatchmanageserver.service.ApiImageUploadService;
import com.online.messagebatchmanageserver.service.MessageService;
import net.sf.json.JSONObject;
import org.apache.commons.collections.map.HashedMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.support.StandardMultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedDeque;
import java.util.concurrent.ConcurrentLinkedQueue;

/**
 * Created by littleheng on 2018/4/28
 */
@RestController
public class MessageBatchController {

    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private MessageService messageService;

    @Autowired
    private ApiImageUploadService apiImageUploadService;

    /**
     * 保存顾客message到批处理服务接口
     * @return result  0：成功 1：失败
     */
    @RequestMapping(value = "/saveCustomerMessage", method = RequestMethod.POST)
    public Map saveCustomerMessage(@RequestBody Map map) {

        System.out.println("map:"+map);
        String uuid = map.get("uuid").toString();
        JSONObject json = JSONObject.fromObject( map.get("message"));

        Message message = new Message();
        message.setUuid(json.get("id").toString());
        message.setContent(json.get("content").toString());

        String time = json.get("createTime").toString();
        long ltime = Long.parseLong(time);
        message.setCreateTime(new Timestamp(ltime));
        message.setDirection(Integer.parseInt(json.get("direction").toString()));
        message.setCompanyId(json.get("cId").toString());
        message.setReadStatus(Integer.parseInt(json.get("loadStatus").toString()));
        message.setSessionId(json.get("sId").toString());
        message.setType(Integer.parseInt(json.get("type").toString()));


        Map<String, Object> maps = new HashMap<String, Object>();
        if ("".equals(uuid) || message == null) {
            this.logger.info("saveCustomerMessage：参数为空");
            maps.put("result", 1);
            return maps;
        }

        if (message.getType() == 2 ) {
            Map<String, String> param = new HashMap<>();
            param.put("mediaId", message.getContent());
            Map result = this.apiImageUploadService.uploadFromWeChat(param);
            if ((Integer)result.get("result") == 0) {
                message.setContent(result.get("url").toString());
                maps.put("url", result.get("url"));
            } else {
                this.logger.info("图片未上传成功");
                maps.put("result", 1);
                return maps;
            }
        }

        this.messageService.saveCustomerMessage(uuid, message);
        maps.put("result", 0);
        System.out.println("restMap:"+maps);
        return maps;
    }

    /**
     * 保存坐席message到批处理服务接口
     * @return result  0：成功 1：失败
     */
    @RequestMapping(value = "/saveAgentMessage", method = RequestMethod.POST)
    public Map saveAgentMessage(@RequestBody Map map) {

        String uuid = map.get("uuid").toString();
        JSONObject json = JSONObject.fromObject( map.get("message"));

        Message message = new Message();
        message.setUuid(json.get("id").toString());
        message.setContent(json.get("content").toString());
        String time = json.get("createTime").toString();
        long ltime = Long.parseLong(time);
        message.setCreateTime(new Timestamp(ltime));
        message.setDirection(Integer.parseInt(json.get("direction").toString()));
        message.setCompanyId(json.get("cId").toString());
        message.setReadStatus(Integer.parseInt(json.get("loadStatus").toString()));
        message.setSessionId(json.get("sId").toString());
        message.setType(Integer.parseInt(json.get("type").toString()));

        Map<String, Object> maps = new HashMap<>();
        if ("".equals(uuid) || message == null) {
            this.logger.info("saveAgentMessage：参数为空");
            maps.put("result", 1);
            return maps;
        }

//        if (message.getType() == 2 ) {
//            Map paths = (Map)this.apiImageUploadService.upload(message.getContent(), null);
//            message.setContent(paths.get("url").toString());
//        }
        this.messageService.saveAgentMessage(uuid, message);
        maps.put("result", 0);
        return maps;
    }

    /**
     * 保存已读的uuid和企业id到批处理服务接口
     * @return result   0：成功 1：失败
     */
    @RequestMapping(value = "/saveUuids", method = RequestMethod.POST)
    public Map saveUuids(@RequestBody Map map) {

        String[] uuids = (String[])map.get("uuids");
        Integer eid = (Integer)map.get("e_id");

        Map<String, Object> maps = new HashMap<>();
        if (uuids.length <= 0 || "".equals(eid)) {
            this.logger.info("saveUuids：参数为空");
            maps.put("result", 1);
            return maps;
        }
        this.messageService.saveUuid(uuids, eid);
        maps.put("result", 0);
        return maps;
    }
}